package com.mright.controller.account;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mright.controller.base.BaseController;
import com.mright.entity.SysUser;
import com.mright.model.LoginModel;
import com.mright.service.AccountService;


@Controller
public class AccountController extends BaseController {
	
	@Autowired
	private AccountService accountService;
	
	@GetMapping("/login")
	public String login()
	{
		return "account/login";
	}
	
	@PostMapping("/login")
	public String login(LoginModel loginModel)
	{
		Subject subject = getSubject();
        if (subject != null)
            subject.logout();
		UsernamePasswordToken token = new UsernamePasswordToken(loginModel.getUserName(), loginModel.getPassWord(),false);
		this.login(token);
		return "redirect:/index";
	}
	
    @RequestMapping("/")
    public String redirectIndex() {
        return "redirect:/index";
    }
    
    @GetMapping("/403")
    public String forbid() {
        return "403";
    }
	
    @RequestMapping("/index")
    public String index(Model model) {
        // 登录成后，即可通过 Subject 获取登录的用户信息
        SysUser user = super.getCurrentUser();
        model.addAttribute("user", user);
        return "index";
    }


}
